package com.analysis.service.impl;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.analysis.common.ConfigUtil;
import com.analysis.service.DataSetService;

@Service
public class DataSetServiceImpl implements DataSetService {

    private static Map<String, List<String>> dataSet = new HashMap<>();
    private static List<String> searchData = new ArrayList<>();
    
    
    static {
        if (dataSet.isEmpty()) {
            File klineFile = Paths.get("./kline/dataSet.txt").toFile();
            if (klineFile.exists()) {
                String content = null;
                try {
                    content = FileUtils.readFileToString(klineFile, Charset.defaultCharset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (StringUtils.isNotEmpty(content)) {
                    dataSet = JSONObject.parseObject(content, HashMap.class);
                }
            }
        }
    }
    
    public static Map<String, List<String>> getDataSet(){
    	return dataSet;
    }

    @Override
    public Map<String, List<String>> readDataSet() {
        if (dataSet.isEmpty()) {
            File klineFile = Paths.get("./kline/dataSet.txt").toFile();
            if (klineFile.exists()) {
                String content = null;
                try {
                    content = FileUtils.readFileToString(klineFile, Charset.defaultCharset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (StringUtils.isNotEmpty(content)) {
                    dataSet = JSONObject.parseObject(content, HashMap.class);
                }
            }
        }
        return dataSet;
    }

    public List<String> getSpecificDataSet(String name) {
        for (Entry<String, List<String>> entry : dataSet.entrySet()) {
            String key = entry.getKey();
            List<String> value = entry.getValue();
            if (key.equalsIgnoreCase(name)) {
                return value;
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> getSearchData() {
        if (searchData.isEmpty()) {
            File klineFile = Paths.get("./kline/searchData.txt").toFile();
            if (klineFile.exists()) {
                String content = null;
                try {
                    content = FileUtils.readFileToString(klineFile, Charset.defaultCharset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (StringUtils.isNotEmpty(content)) {
                    searchData = JSONObject.parseObject(content, ArrayList.class);
                }
            }
        }
        return searchData;
    }

    @Override
    public List<String> getRandomDataSet() {
        if ("true".equalsIgnoreCase(ConfigUtil.getString("randomSet"))) {
            List<String> symbols = getSearchData();
            List<String> data = new ArrayList<>();
            while (data.size() <= 50) {
                int index = RandomUtils.nextInt();
                if (index > 0 && index < symbols.size() - 1) {
                    data.add(symbols.get(index).split(",")[0]);
                }
            }
            return data;
        } else {
            // List<String> list = Arrays.asList("sz002245", "sh601799",
            // "sz002228", "sh603038", "sz300717", "sz000968",
            // "sh600996", "sh600674", "sh603633", "sz000800", "sz000881");
            List<String> list = Arrays.asList("sh600009");
            return list;
        }
    }

    private void saveDataSet() {
        if (!dataSet.isEmpty()) {
            File dataSet = Paths.get("./kline/dataSet.txt").toFile();
            try {
                FileUtils.writeStringToFile(dataSet, JSONObject.toJSONString(dataSet), Charset.defaultCharset(),
                        false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void randomDataSet() {
        for (int i = 0; i < 10; i++) {
            String name = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 5);
            dataSet.put(name, getRandomDataSet());
        }
    }

    public static void main(String[] args) {
        DataSetServiceImpl set = new DataSetServiceImpl();
        for (Entry<String, List<String>> entry : set.readDataSet().entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
        
        // set.randomDataSet();
        // set.saveDataSet();
    }
}
